home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / muds / pennmush.000 / pennmush-1.50-p8-linux.tar / pennmush / player_list.c < prev    next >
C/C++ Source or Header  |  1993-04-06  |  3KB  |  165 lines

  1. /* player_list.c */
  2.  
  3. #include <ctype.h>
  4. #include "copyright.h"
  5.  
  6. #include "config.h"
  7. #include "db.h"
  8. #include "interface.h"
  9. #include "globals.h"
  10. #include "externs.h"
  11.  
  12.  
  13. #define PLAYER_LIST_SIZE (1 << 12)    /* must be a power of 2 */
  14.  
  15. static dbref hash_function_table[256];
  16. static int hft_initialized = 0;
  17.  
  18. struct pl_elt {
  19.   dbref player;            /* pointer to player */
  20.                 /* key is db[player].name or alias */
  21.   struct pl_elt *next;
  22. };
  23.  
  24. static void init_hft()
  25. {
  26.   int i;
  27.  
  28.   for (i = 0; i < 256; i++) {
  29.     hash_function_table[i] = random() & (PLAYER_LIST_SIZE - 1);
  30.   }
  31.   hft_initialized = 1;
  32. }
  33.  
  34. static dbref hash_function(string)
  35.     const char *string;
  36. {
  37.   dbref hash;
  38.   if (!hft_initialized)
  39.     init_hft();
  40.   hash = 0;
  41.   for (; *string; string++) {
  42.     hash ^= ((hash >> 1) ^ hash_function_table[DOWNCASE(*string)]);
  43.   }
  44.   return (hash);
  45. }
  46.  
  47. static struct pl_elt *player_list[PLAYER_LIST_SIZE];
  48. static int pl_used = 0;
  49. static struct pl_elt *palias_list[PLAYER_LIST_SIZE];
  50. static int al_used = 0;
  51.  
  52. void clear_players()
  53. {
  54.     int i;
  55.     struct pl_elt *e, *next;
  56.  
  57.     for (i = 0; i < PLAYER_LIST_SIZE; i++) {
  58.     if (pl_used) {
  59.         for (e = player_list[i]; e; e = next) {
  60.         next = e->next;
  61.         free((char *) e);
  62.         }
  63.     }
  64.     if (al_used) {
  65.         for (e = palias_list[i]; e; e = next) {
  66.         next = e->next;
  67.         free(e);
  68.         }
  69.     }
  70.     player_list[i] = palias_list[i] = 0;
  71.     }
  72.     pl_used = al_used = 1;
  73. }
  74.  
  75. void add_player(player, alias)
  76.      dbref player;
  77.      char *alias;
  78. {
  79.     dbref hash;
  80.     struct pl_elt *e;
  81.  
  82.     e = (struct pl_elt *) malloc(sizeof(struct pl_elt));
  83.     e->player = player;
  84.     if (alias == NULL) {
  85.     hash = hash_function(Name(player));
  86.     e->next = player_list[hash];
  87.     player_list[hash] = e;
  88.     } else {
  89.     hash = hash_function(alias);
  90.     e->next = palias_list[hash];
  91.     palias_list[hash] = e;
  92.     }
  93. }
  94.  
  95. dbref lookup_player(name)
  96.     const char *name;
  97. {
  98.     struct pl_elt *e;
  99.     dbref hash;
  100.     ATTR *a;
  101.     char alias[PLAYER_NAME_LIMIT];
  102.  
  103.     hash = hash_function(name);
  104.  
  105.     for (e = player_list[hash]; e; e = e->next) {
  106.     if (!strcasecmp(Name(e->player), name))
  107.         return e->player;
  108.     }
  109.     for (e = palias_list[hash]; e; e = e->next) {
  110.     if ((a = atr_get_noparent(e->player, "ALIAS")) != NULL) {
  111.         strcpy(alias, uncompress(a->value));
  112.         if (!strcasecmp(alias, name))
  113.         return e->player;
  114.     }
  115.     }
  116.  
  117.     return NOTHING;
  118. }
  119.  
  120. void delete_player(player, alias)
  121.      dbref player;
  122.      char *alias;
  123. {
  124.     dbref hash;
  125.     struct pl_elt *prev, *e;
  126.  
  127.     if (alias == NULL) {
  128.     hash = hash_function(Name(player));
  129.     if ((e = player_list[hash]) == 0) {
  130.         return;
  131.     } else if (e->player == player) {
  132.         /* it's the first one */
  133.         player_list[hash] = e->next;
  134.         free((char *) e);
  135.     } else {
  136.         for (prev = e, e = e->next; e; prev = e, e = e->next) {
  137.         if (e->player == player) {
  138.             /* got it */
  139.             prev->next = e->next;
  140.             free((char *) e);
  141.             break;
  142.         }
  143.         }
  144.     }
  145.     } else {
  146.     hash = hash_function(alias);
  147.     if ((e = palias_list[hash]) == 0) {
  148.         return;
  149.     } else if (e->player == player) {
  150.         /* it's the first one */
  151.         palias_list[hash] = e->next;
  152.         free((char *) e);
  153.     } else {
  154.         for (prev = e, e = e->next; e; prev = e, e = e->next) {
  155.         if (e->player == player) {
  156.             /* got it */
  157.             prev->next = e->next;
  158.             free((char *) e);
  159.             break;
  160.         }
  161.         }
  162.     }
  163.     }
  164. }
  165.